package edu.nf.waimai.dao.admin.impl;

import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.admin.AdminRiderdao;
import edu.nf.waimai.model.rider.Rider;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-09-12
 * @Author qiu
 */
public class AdminRiderDaoImpl implements AdminRiderdao {
    @Override
    public List<Rider> getAllRider() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select rider_id,rider_name,rider_phone,rider_type,rider_datetime from rider";

        BeanListHandler<Rider> userBeanHandler = new BeanListHandler<>(Rider.class);

        return sqlExecutor.executeQuery(sql, userBeanHandler);
    }

    @Override
    public int updateRider(Rider rider) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "update rider set rider_name = ?,rider_phone=?, where rider_id = ?";
        return sqlExecutor.executeUpdate(sql, rider.getRiderName(),rider.getRiderPhone(),rider.getRiderId());
    }

    @Override
    public int deleteRider(int uid) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "delete from rider where rider_id = ?";
        return sqlExecutor.executeUpdate(sql, uid);
    }

    @Override
    public int addRider(Rider rider) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "insert into rider(rider_name,rider_phone) value(?,?)";
        return sqlExecutor.executeUpdate(sql,rider.getRiderName(),rider.getRiderPhone());
    }

    @Override
    public List<Rider> getRider() {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select order_id,u_name,shop_name,province,city,district,detail,rider_name,total_price,order_type,order_datetime,order_remarks from orders \n" +
                " left join users on users.u_id = orders.u_id\n" +
                " left join shop on shop.shop_id = orders.shop_id\n" +
                " left join address on address.address_id = orders.address_id\n" +
                " left join rider on rider.rider_id = orders.rider_id \n" +
                " where order_status=6;";
        BeanListHandler<Rider> userHandler = new BeanListHandler<>(Rider.class);
        return sqlExecutor.executeQuery(sql, userHandler);
    }
}
